#!/bin/bash
TMP_FILE="/tmp/$(date +%m-%d-%H-%M-%S).tmp";
LAST_INDEX=$#;
LAST_BEFORE_INDEX=$LAST_INDEX
OUT=${@:$LAST_INDEX};
((LAST_BEFORE_INDEX -= 1))

function statistic {
	f=$1;
	out=$TMP_FILE;
	port=$3;
	extend='';
	if [ $# -gt 1 ];
	then
			out=$2;
	fi

	if [ $# -gt 2 ];
	then
			extend=" | awk '{print $1 \":$port\"}' ";
	fi

	echo "$(cat $f  | sort -n | uniq $extend >> $out)";
}

for file in ${@:1:$LAST_BEFORE_INDEX};
do
				p="$(awk -F : '{print $1}' <<< $file)";

				echo "statistic : $file ==> $TMP_FILE"; 
				statistic $file $TMP_FILE $p;
done

echo "statistic : $TMP_FILE ==> $OUT"; 
statistic $TMP_FILE  $OUT;

#echo ${@:$LAST_INDEX};
